"""
Case Type   : SQL_DCL
Case Name   : 多线程并发使用ALTER SYSTEM SET命令修改postgresql.conf文件
Create At   : 2022/08/12
Owner       : liu-tong-8848
Description :
    1.线程1使用alter system set设置guc参数
    2.线程2使用alter system set设置guc参数
    3.查询数据库状态
Expect      :
    1.设置成功
    2.设置成功
    3.数据库状态正常
History     :
"""
import os
import unittest
from testcase.utils.Logger import Logger
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.ComThread import ComThread
from yat.test import Node


class ClassName(unittest.TestCase):
    def setUp(self):
        self.logger = Logger()
        self.logger.info(f"-----{os.path.basename(__file__)} start-----")
        self.user_node = Node('PrimaryDbUser')
        self.primary_sh = CommonSH('PrimaryDbUser')
        self.constant = Constant()

    def test_testcase_name(self):
        text1 = "-----step1:线程1使用alter system set设置guc参数;expect:设置成功-----"
        self.logger.info(text1)
        sql_cmd = f"alter system set archive_mode to on;" \
            f"select pg_sleep(2);" \
            f"show archive_mode;" \
            f"alter system set archive_mode to off;" \
            f"select pg_sleep(2);" \
            f"show archive_mode;"
        self.logger.info(sql_cmd)
        threads = []
        thread_1 = ComThread(self.primary_sh.execut_db_sql, args=(sql_cmd,))
        thread_1.setDaemon(True)
        threads.append(thread_1)

        text2 = "-----step2:线程2使用alter system set设置guc参数;expect:设置成功-----"
        self.logger.info(text2)
        sql_cmd = f"alter system set archive_timeout to 600;" \
            f"select pg_sleep(2);" \
            f"show archive_timeout;" \
            f"alter system set archive_timeout to 0;" \
            f"select pg_sleep(2);" \
            f"show archive_timeout;"
        self.logger.info(sql_cmd)
        thread_2 = ComThread(self.primary_sh.execut_db_sql, args=(sql_cmd,))
        thread_2.setDaemon(True)
        threads.append(thread_2)
        for thread in threads:
            thread.start()
        for thread in threads:
            thread.join(60)
            self.logger.info(thread.get_result())
            self.assertEquals(2, thread.get_result().count(
                self.constant.alter_system_success_msg), '执行失败' + text1)
            if thread == thread_1:
                self.assertIn('on', thread.get_result().splitlines()[8],
                              '执行失败' + text1)
                self.assertIn('off', thread.get_result().splitlines()[-2],
                              '执行失败' + text1)
            else:
                self.assertIn('10min', thread.get_result().splitlines()[8],
                              '执行失败' + text1)
                self.assertIn('0', thread.get_result().splitlines()[-2],
                              '执行失败' + text1)

        text = "-----step3:查询数据库状态;expect:数据库状态正常-----"
        self.logger.info(text)
        status = self.primary_sh.get_db_cluster_status('status')
        self.logger.info(status)
        self.assertTrue(status, '数据库状态异常')

    def tearDown(self):
        self.logger.info("-----无需清理环境-----")
        self.logger.info(f"-----{os.path.basename(__file__)} end-----")
